////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.EntityClasses;
using RPSDAL.FactoryClasses;
using RPSDAL.DaoClasses;
using RPSDAL.HelperClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;

public partial class ContactPageAdministration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
            if (Parameters.LoggedUser != -1)
            {
                UserEntity eUser = new UserEntity(Parameters.LoggedUser);
                RoleEntity eRole = new RoleEntity(eUser.RoleId);

                if (!eRole.HasConfigureContactInterface)
                {
                    Parameters.LoggedUser = -1;
                    Response.Redirect("Default.aspx");
                    return;
                }
            }

            btnRemoveFields.Attributes.Add("OnClick", "return ValidateDelete()");
            ContactPageFieldCollection cContactPageField = new ContactPageFieldCollection();
            cContactPageField.GetMulti(null,0,new SortExpression(new SortClause(ContactPageFieldFields.OrderIndex,SortOperator.Ascending)));

            grdPageFields.DataSource = cContactPageField;
            grdPageFields.DataBind();
            txtHidden.Value = cContactPageField.Count.ToString();

            for (int i = 0; i < grdPageFields.Rows.Count; i++) 
            {
                try
                {
                    switch (Convert.ToInt32(grdPageFields.Rows[i].Cells[2].Text))
                    {
                        case Parameters.TEXTBOX_TYPE:
                            //textbox
                            TextBox txtTextBox = new TextBox();
                            grdPageFields.Rows[i].Cells[2].Controls.Add(txtTextBox);
                            break;
                        case Parameters.COMBOBOBOX_TYPE:
                            // combobox
                            ContactPageDropdownFieldValueCollection cComboboxValues = new ContactPageDropdownFieldValueCollection();
                            IPredicateExpression filter = new PredicateExpression();
                            filter.Add(ContactPageDropdownFieldValueFields.ContactPageFieldId == Convert.ToInt32(((TextBox)grdPageFields.Rows[i].Cells[6].FindControl("id")).Text));
                            cComboboxValues.GetMulti(filter);

                            DropDownList cmbComboBox = new DropDownList();
                            cmbComboBox.DataSource = cComboboxValues;
                            cmbComboBox.DataValueField = ContactPageDropdownFieldValueFields.ContactPageFieldId.Name;
                            cmbComboBox.DataTextField = ContactPageDropdownFieldValueFields.Value.Name;
                            cmbComboBox.DataBind();

                            grdPageFields.Rows[i].Cells[2].Controls.Add(cmbComboBox);
                            break;
                        case Parameters.CHECKBOX_TYPE:
                            CheckBox ckCehckBox = new CheckBox();
                            grdPageFields.Rows[i].Cells[2].Controls.Add(ckCehckBox);
                            break;
                        case Parameters.TEXTAREA_TYPE:
                            TextBox txtTextArea = new TextBox();
                            txtTextArea.TextMode = TextBoxMode.MultiLine;
                            grdPageFields.Rows[i].Cells[2].Controls.Add(txtTextArea);
                            break;
                        case Parameters.DATETIME_TYPE:
                            TextBox txtDateTime = new TextBox();
                            txtDateTime.ID = "txtDateTimeId";
                            grdPageFields.Rows[i].Cells[2].Controls.Add(txtDateTime);
                            Image imgDateTime = new Image();
                            imgDateTime.ImageUrl = "jscripts/date/calendar.gif";
                            imgDateTime.ID = "dateId";
                            grdPageFields.Rows[i].Cells[2].Controls.Add(imgDateTime);
                            break;
                    }
                }
                catch { }
                
            }
        }
    }
    protected void AddNewField(object sender, EventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Add New Filed Contact Page");
        Response.Redirect("ContactPageField.aspx");
    }
   
    /// <summary>
    /// modify the order in which the fields appear
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ModifyFieldOrder(object sender, EventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Modify Filed Order From Contact Page");
        //try
        //{
        //    for (int i = 0; i < grdPageFields.Rows.Count; i++)
        //    {
        //        ContactPageFieldEntity eContactPageField = new ContactPageFieldEntity(Convert.ToInt32(((TextBox)grdPageFields.Rows[i].Cells[6].FindControl("id")).Text));
        //        eContactPageField.OrderIndex = Convert.ToInt32(((TextBox)grdPageFields.Rows[i].Cells[3].FindControl("txtOrder")).Text);
        //        eContactPageField.Save();
        //    }
        //}
        //catch { }

        //Response.Redirect("ContactPageAdministration.aspx");

        for (int i = 0; i < grdPageFields.Rows.Count; i++)
        {
            int id = 0;
            int orderIndex = 0;

            if (Int32.TryParse(((TextBox)grdPageFields.Rows[i].Cells[3].FindControl("txtOrder")).Text, out orderIndex)
                && Int32.TryParse(((TextBox)grdPageFields.Rows[i].Cells[6].FindControl("id")).Text, out id))
            {
                ContactPageFieldEntity eContactPageField = new ContactPageFieldEntity(id);
                if (eContactPageField.OrderIndex != orderIndex)
                {
                    eContactPageField.OrderIndex = orderIndex;
                    eContactPageField.Save();
                }
            }
        }
        ContactPageFieldCollection cContactPageFields = new ContactPageFieldCollection();

        cContactPageFields.GetMulti(null, 0, new SortExpression(new SortClause(ContactPageFieldFields.OrderIndex, SortOperator.Ascending)));


        for (int i = 0; i < cContactPageFields.Count; i++)
        {
            cContactPageFields[i].OrderIndex = i + 1;
            cContactPageFields[i].Save();
        }

        Response.Redirect("ContactPageAdministration.aspx");

    }

    /// <summary>
    /// remove fields from the contact page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void RemoveFields(object sender, EventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Remove Filed From Contact Page");
        ContactPageFieldCollection cDeleteContactPageFields = new ContactPageFieldCollection();
        for (int i = 0; i < grdPageFields.Rows.Count; i++)
        {
            CheckBox ckRemove = (CheckBox)(grdPageFields.Rows[i].Cells[5].FindControl("ckRemove"));
            if (ckRemove != null && ckRemove.Checked) 
            {
                // delete field
                try
                {
                    ContactPageFieldEntity eContactPageField = new ContactPageFieldEntity(Convert.ToInt32(((TextBox)grdPageFields.Rows[i].Cells[6].FindControl("id")).Text));
                    cDeleteContactPageFields.Add(eContactPageField);
                }
                catch { }
            }
        }
        cDeleteContactPageFields.DeleteMulti();
        Response.Redirect("ContactPageAdministration.aspx");
    }


    protected void ModifyField(object sender, GridViewCommandEventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Edit Filed From Contact Page id="+e.CommandArgument.ToString());
        Response.Redirect("ContactPageField.aspx?id=" + Global.EncryptString(e.CommandArgument.ToString()));
    }
}
